//
//  AXFSuperModel.swift
//  SpuerYangAngWang
//
//  Created by 杨茂盛 on 2016/12/22.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit

class AXFSuperModel: NSObject {
    
    //记录 id
    var idString:String?
    //头像
    var icon:String?
    //名字
    var name:String?
    //id
    var id:Int?
    var sort:Int?
    var pcid:Int?
    var visibility:Int?
    var disabled_show:Int?
    var products:[ZHSellFastModel] = [ZHSellFastModel]()
    
    //单例
    static let shared:AXFSuperModel = AXFSuperModel()

    
    func loadData(finished:@escaping (Bool,[AXFSuperModel]) ->()){
        let urlString = "http://iosapi.itcast.cn/loveBeen/supermarket.json.php"
        let parameters = ["call":"5"]
        YMSNetWorkToll.shared.request(method: .POST, urlString: urlString, parameters: parameters) { (responed, error) in
            if(error != nil){
                return;
            }
            var dict = responed as! [String:Any]
            var data:[String:Any] = dict["data"] as! [String : Any]
            let array:[Any] = data["categories"] as! [Any]
            
            //右边的数据
            let rightDict = data["products"] as! [String: Any]
            
            var leftTempArray = [AXFSuperModel]()
            
            for item in array{
                let model = AXFSuperModel()
                model.yy_modelSet(withJSON: item)
                //取出其中的 id
                let dict = item as! [String:Any]
                self.idString = dict["id"] as! String?
                //                 leftTempArray.append(model)
                //取出products中的数据
                model.products = [ZHSellFastModel]()
                for item2 in rightDict[self.idString!] as! [Any]{
                    let productsModel = ZHSellFastModel()
                    productsModel.yy_modelSet(withJSON: item2)
                    
                    model.products.append(productsModel)
                }
                leftTempArray.append(model)
            }
            finished(true,leftTempArray)
        }
    }
}
